package com.hp.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import redis.clients.jedis.Jedis;

public class SendSms {
    public static void main(String[] args) {
        sendSms("13462571103");
    }

    public static String verifyCode(){
        return String.valueOf((int)(Math.random()*900000 + 100000));
    }

    public static String sendSms(String phone){
        /**
        *@author zh
        *@Date 2020/12/30
        *@MethodNotes    "<your-region-id>",          // 您的地域ID
         *             "<your-access-key-id>",      // 您的AccessKey ID
         *             "<your-access-key-secret>"); // 您的AccessKey Secret
        **/
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GEzxfxeoradzxeFThe3", "");
        IAcsClient client = new DefaultAcsClient(profile);
        //生成随机验证码
        String verifyCode = verifyCode();
        //请求
        CommonRequest request = new CommonRequest();
        //设置请求方法提交方式
        request.setSysMethod(MethodType.POST);
        //该产品的服务地址(开发者无需替换) 阿里云的api
        request.setSysDomain("dysmsapi.aliyuncs.com");
        //api版本号
        request.setSysVersion("2017-05-25");
        //请求方法路径
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        //连接超时时间
        //request.putQueryParameter("connectTimeout", );
        //读取超时时间
        //request.putQueryParameter("readTimeout", );
        //待发送手机号
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "傈傈音乐");
        request.putQueryParameter("TemplateCode", "SMS_207964345");
        //短信验证码
        request.putQueryParameter("TemplateParam", "{\"code\":\""+ verifyCode +"\"}");
        System.out.println("验证码为：" + verifyCode);
        Jedis jedis = new Jedis("localhost", 6379);
        jedis.setex(phone, 60,verifyCode);
        jedis.close();
        try {
            //
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }
}
